package com.rongke.service.impl;

import java.util.ArrayList;
import java.util.List;

import jodd.util.StringUtil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rongke.mapper.MedicineMapper;
import com.rongke.model.Medicine;
import com.rongke.service.MedicineService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Service;

/**
 * @MedicineServiceImpl
 * @药物ServiceImpl
 * @version : Ver 1.0
 */
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements
                                                                              MedicineService {

    @Override
    public Page<Medicine> selectPageByTypeId(Page<Medicine> page, int typeId) {
        EntityWrapper<Medicine> ew = new EntityWrapper<>();
        ew.eq("type_id", typeId);
        List<Medicine> inforPage = baseMapper.selectPage(page, ew);
        page.setRecords(inforPage);
        return page;
    }

    @Override
    public List<Medicine> search(String[] keys, int limit) {
        if (ArrayUtils.isEmpty(keys)) {
            return null;
        }

        List<String> keyList = new ArrayList<String>();
        for (String key : keys) {
            if (StringUtil.isEmpty(key)) {
                continue;
            }
            keyList.add(key);
        }
        if (CollectionUtils.isEmpty(keyList)) {
            return null;
        }

        EntityWrapper<Medicine> ew = new EntityWrapper<>();
        for (int i = 0; i < keyList.size(); i++) {
            if (i != 0) {
                ew.or();
            }
            ew.like("medicine_name", keyList.get(i));
        }
        ew.last("limit " + String.valueOf(limit));
        List<Medicine> result = baseMapper.selectList(ew);
        return result;
    }
}
